﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LIMS.Shared;
using LIMS.Entities;

namespace LIMS.Windows
{
    public partial class UserProfile : Form
    {
        public UserProfile()
        {
            InitializeComponent();
        }

        private void UserProfile_Load(object sender, EventArgs e)
        {

            try
            {
                BindUserType();
                BindStatus();
                LoadUserData();
                EnableDisableControls(false);
            }
            catch (Exception ex)
            { }
        }

        private void LoadUserData()
        {
            try
            {

                txtFirstName.Text = SharedResource.gLoggedinUser.FirstName;
                txtLastName.Text = SharedResource.gLoggedinUser.LastName;
                txtUserName.Text = SharedResource.gLoggedinUser.UserName;
                txtEmail.Text = SharedResource.gLoggedinUser.Email;
                txtEmployeeCode.Text = SharedResource.gLoggedinUser.EmployeeCode;
                txtdateTime.Text = Convert.ToString(SharedResource.gLoggedinUser.UserDOB);
                
                cmbUserType.SelectedValue = SharedResource.gLoggedinUser.UserType;
                cmbStatus.SelectedValue = SharedResource.gLoggedinUser.Status;
                
            }
            catch (Exception ex)
            { }
        }

        /// <summary>
        /// Enable disable form controls on action basis.
        /// if bValue is true contrlos will be enabled else disabled.
        /// </summary>
        /// <param name="bValue"></param>
        private void EnableDisableControls(bool bValue)
        {
            try
            {
                txtFirstName.Enabled = bValue;
                txtLastName.Enabled = bValue;
                txtEmail.Enabled = bValue;
                txtUserName.Enabled = bValue;
                cmbUserType.Enabled = bValue;
                txtEmployeeCode.Enabled = bValue;
                txtdateTime.Enabled = bValue;
                cmbStatus.Enabled = bValue;
                btnSaveUser.Enabled = bValue;

            }
            catch (Exception ex)
            { }
        }

        private void btnEditProfile_Click(object sender, EventArgs e)
        {
            try
            {
                EnableDisableControls(true);
            }
            catch (Exception ex)
            { }
        }

        private void BindUserType()
        {
            try
            {
                //Dictionary<int, string> oUserType = Resource.UserTypeDic;
                //int iUserType;
                //iUserType = SharedResource.gLoggedinUser.UserType;
                //switch (iUserType)
                //{

                //    case 1:
                //        oUserType.Remove(1);
                //        cmbUserType.DataSource = new BindingSource(oUserType, "");
                //        break;
                //    case 2:
                //        oUserType.Remove(1);
                //        oUserType.Remove(2);
                //        cmbUserType.DataSource = new BindingSource(oUserType, "");
                //        break;
                //    case 3:
                //        oUserType.Remove(1);
                //        oUserType.Remove(2);
                //        oUserType.Remove(3);
                //        cmbUserType.DataSource = new BindingSource(oUserType, "");
                //        break;
                //    case 4:
                //        oUserType.Remove(1);
                //        oUserType.Remove(2);
                //        oUserType.Remove(3);
                //        //oUserType.Remove(4);
                //        cmbUserType.DataSource = new BindingSource(oUserType, "");
                //        break;
                //    default:
                //        cmbUserType.DataSource = new BindingSource(oUserType, "");
                //        break;

                //}
                cmbUserType.DataSource = new BindingSource(Resource.UserTypeDic, "");
                cmbUserType.DisplayMember = "Value";
                cmbUserType.ValueMember = "Key";

            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        private void BindStatus()
        {
            try
            {
                cmbStatus.DataSource = new BindingSource(Resource.UserStatusDic, "");
                cmbStatus.DisplayMember = "Value";
                cmbStatus.ValueMember = "key";

            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                ClearData();
                LoadUserData();
                EnableDisableControls(false);
            }
            catch (Exception ex)
            { }
        }
        private void ClearData()
        {
            try
            {
                txtFirstName.Text = string.Empty;
                txtLastName.Text = string.Empty;
                txtEmail.Text = string.Empty;
                txtUserName.Text = string.Empty;
               
                cmbUserType.SelectedValue = 0;
                txtEmployeeCode.Text = string.Empty;
                cmbStatus.SelectedValue = "X";
                txtdateTime.Text = string.Empty;
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }

        private void btnSaveUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtFirstName.Text.Trim()) || string.IsNullOrEmpty(txtLastName.Text.Trim()) ||
                    string.IsNullOrEmpty(txtEmail.Text.Trim()) || string.IsNullOrEmpty(txtUserName.Text.Trim()) ||
                    string.IsNullOrEmpty(txtEmployeeCode.Text.Trim()) || Convert.ToString(cmbStatus.SelectedValue) == "X" || Convert.ToInt16(cmbUserType.SelectedValue) == 0)
                {
                    MessageBox.Show("Fill mandatory(*) information.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else if (!Validations.IsDate(txtdateTime.Text))
                {
                    MessageBox.Show("Date is not valid.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtdateTime.Focus();
                    return;
                }
                //else if (txtConfirmPassword.Text != txtPassword.Text)
                //{
                //    MessageBox.Show("Passwords do not match.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    txtConfirmPassword.Text = string.Empty;
                //    txtPassword.Text = string.Empty;
                //    txtPassword.Focus();
                //    return;
                //}
                else
                {
                    SharedResource.gLoggedinUser.FirstName = txtFirstName.Text;
                    SharedResource.gLoggedinUser.LastName = txtLastName.Text;
                    SharedResource.gLoggedinUser.Email = txtEmail.Text;
                    SharedResource.gLoggedinUser.UserName = txtUserName.Text;
                    SharedResource.gLoggedinUser.LastModifiedBy = SharedResource.gLoggedinUser.UserType;
                    SharedResource.gLoggedinUser.LastModifiedDateTime = DateTime.Now;
                    SharedResource.gLoggedinUser.Status = Convert.ToString(cmbStatus.SelectedValue);
                    SharedResource.gLoggedinUser.UserType = Convert.ToInt16(cmbUserType.SelectedValue);
                    SharedResource.gLoggedinUser.EmployeeCode = txtEmployeeCode.Text;
                    SharedResource.gLoggedinUser.UserDOB = Convert.ToDateTime(txtdateTime.Text);

                    if (SharedResource.UpdateUser(SharedResource.gLoggedinUser))
                    {
                        MessageBox.Show("User profile updated successfully.", "Update profile", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ClearData();
                        LoadUserData();
                        EnableDisableControls(false);
                    }

                }
            }
            catch (Exception ex)
            {
                //Logger.Error("Error occured.", ex);
            }
        }
    }
}
